********************************************************************************
** Do file: g15_compliance_day200.do
** First started: February 19, 2023 
** Last edited: December 31, 2023

/* Purpose: this do file examines share of days in compliance at day 200 in regression format

Part I: Share of days in compliance at day 200

* ITEM MADE: TABLE A.7
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Compliance

* Load the key dataset
use "$merged/key_rep.dta", clear

* Merge baseline data onto the above
merge m:1 hhid using "$bsvy_clean/hhvars_baseline.dta"
assert _merge!=2
drop _merge

* Develop filter for sampling framework, going all the way to endline
g tag = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_tookloan_repay==1
* Generate a version of tag that does not incorporate control group
g tag_nocont = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_tookloan_repay==1 & treatmenttype_sh!="R C"

* Develop a variable that tags those that are in a main treatment group of interest
g maintreat = 1 if treatmenttype_sh=="R T1-L" | treatmenttype_sh == "R T1-U" | treatmenttype_sh == "R T2-U"

* Develop treatment variables
g locked = (treatmenttype_sh=="R T1-L")
g su = (treatmenttype_sh=="R T1-U")
g unlocked = (treatmenttype_sh=="R T2-U")
g control = (treatmenttype_sh=="R C")

* Drop choice treatment
drop if treatmenttype_sh=="R T3"

* Divide the raw income numbers by 3704 to make USD
foreach var of varlist light_spend_year hhincome_other value_hh_assets total_loans {
	replace `var' = `var'/3704
}

* Generate copies for imputation using the mean
foreach var of varlist accountpercentlocked_may headage headsex headmarried headworks_fam headworks_self headworks_out num_hh n_hhenrolled light_spend_year hhincome_other value_hh_assets any_loans total_loans ever_refused microfloan {
	g II`var' = `var'
}
* Generate imputated values and dummies for missing
foreach var of varlist II* {
	g `var'_dum = (`var'==.)
	egen `var'_mean = mean(`var') if tag==1
	replace `var'=`var'_mean if `var'==.
	drop `var'_mean
}

* Bring in share of days in compliance at day 200
preserve
	use "$repay_clean/fenix_repay_extend_07172020_rep.dta", clear
	keep if loandayselapsed==200
	tempfile complierinfo
	save `complierinfo'
restore
merge 1:1 accountid using `complierinfo', keepusing(complier_share_wupg)
ren complier_share_wupg compliance
keep if _merge==3 | _merge==1



************
** Part I **
************

***** TABLE A.7 ******

* Outcome mean
sum compliance if tag_nocont==1

* Run on treatment variables
eststo: reg compliance locked su if tag_nocont==1 // no diff across arms of the study
reg compliance locked unlocked su if tag_nocont==1, noconstant
test locked==su==unlocked

tabstat compliance if tag_nocont==1, by(treatmenttype_sh)
	
* Run on variables from baseline, with some imputation
eststo: reg compliance IIaccountpercentlocked_may IIheadage IIheadsex IIheadmarried IIheadworks_fam IIheadworks_self IIheadworks_out IInum_hh IIn_hhenrolled IIlight_spend_year IIhhincome_other IIvalue_hh_assets IIany_loans IItotal_loans IIever_refused IImicrofloan if tag_nocont==1

* Generate table
esttab using "$tables/compliance_tab.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Share of Days in Compliance at Day 200\label{compliance_tab}) ///
	label nodepvars mtitles("Compliance" "Compliance") stats(N, fmt(%9.0f)) nonotes ///
	order(locked su) ///
	varlabels(locked "Secured" su "Surprise Unlocked" _cons "Constant", ///
	elist(locked \addlinespace su \addlinespace _cons \addlinespace))
eststo clear
